package com.android.support.lib.universal.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 自定义水平进度条
 * Created by 彭治铭 on 2017/6/20.
 */

public class ProgressBarHorizontal extends View {
    public ProgressBarHorizontal(Context context) {
        super(context);
        onCreate();
    }

    public ProgressBarHorizontal(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        onCreate();
    }

    private Paint paint;

    private void onCreate() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        setLayerType(View.LAYER_TYPE_HARDWARE, paint);
    }

    private Bitmap srcBitmap, dstBitmap;

    /**
     * 设置位图，提醒，在调用此方法前，最好先对View进行适配。
     *
     * @param srcBitmap 前景
     * @param dstBitmap 后景【背景】
     */
    public void setBitmap(Bitmap srcBitmap, Bitmap dstBitmap) {
        this.srcBitmap = Bitmap.createScaledBitmap(srcBitmap, getLayoutParams().width, getLayoutParams().height, true);
        this.dstBitmap = Bitmap.createScaledBitmap(dstBitmap, getLayoutParams().width, getLayoutParams().height, true);
        ;
    }

    private float progress = 0, max = 100;

    public void setProgress(float progress) {
        this.progress = progress;
        if (this.progress > max) {
            this.progress = max;
        }
        postInvalidate();
    }

    public void setMax(float max) {
        this.max = max;
        postInvalidate();
    }

    public float getProgress() {
        return progress;
    }

    public float getMax() {
        return max;
    }

    public void incrementProgressBy(int diff) {
        setProgress(progress + diff);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        //Log.e("test", "宽:\t" + getWidth() + "\t高:\t" + getHeight());
        if (dstBitmap != null && !dstBitmap.isRecycled()) {
            canvas.drawBitmap(dstBitmap, 0, 0, paint);
        } else {
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            //默认设置灰色背景
            paint.setColor(Color.parseColor("#C0C0C0"));
            canvas.drawLine(getHeight() / 2, getHeight() / 2, getWidth() - (getHeight() / 2), getHeight() / 2, paint);
        }
        canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
        paint.setStrokeWidth(getHeight());
        if (dstBitmap != null && !dstBitmap.isRecycled()) {
            canvas.drawLine(0, getHeight() / 2, getWidth() * (progress / max), getHeight() / 2, paint);
        } else {
            canvas.drawLine(getHeight() / 2, getHeight() / 2, (getWidth() - (getHeight() / 2)) * (progress / max), getHeight() / 2, paint);
        }
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        if (srcBitmap != null && !srcBitmap.isRecycled()) {
            canvas.drawBitmap(srcBitmap, 0, 0, paint);
        } else {
            //默认设置浅蓝色前景
            paint.setColor(Color.parseColor("#4DA6FF"));
            canvas.drawLine(getHeight() / 2, getHeight() / 2, getWidth() - (getHeight() / 2), getHeight() / 2, paint);
        }
        paint.setXfermode(null);
    }
}
